package gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.LinkedList;
import java.util.List;

import hcs.HouseController;
import hcs.model.Room;

import javax.swing.JFrame;

import activity.SetClockActivity;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {

	private List<RoomMonitor> roomMonitors;
	private EnvironmentMonitor environmentMonitor;
	private HouseController houseController;
	
	public MainFrame(HouseController hc) {
		super("Heating Control System");
		this.houseController = hc;
		roomMonitors = new LinkedList<RoomMonitor>();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setJMenuBar(new CommandMenuBar(this));
        GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		this.setLayout(gridbag);
		this.setBackground(Color.WHITE);
		
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;

		int i = 0;
		for(Room r : hc) {
			RoomMonitor rm = new RoomMonitor(this, r);
			roomMonitors.add(rm);
			gridbag.setConstraints(rm, c);
			this.add(rm);
			i += 1;
			if(i % 3 == 0) {
				c.gridwidth = GridBagConstraints.REMAINDER;
			}
			if(i % 4 == 0) {
				i = 0;
				c.gridwidth = GridBagConstraints.BOTH;
			}
		}
		c.gridwidth = GridBagConstraints.REMAINDER;
		environmentMonitor = new EnvironmentMonitor(this, hc.getEnvironment());
		gridbag.setConstraints(environmentMonitor, c);
		this.add(environmentMonitor);
		this.pack();
		this.setResizable(false);
		this.setVisible(true);
		
		SetClockActivity clkSet = new SetClockActivity();
		clkSet.setDelay(1000);
		clkSet.start();
	}
	
	public List<RoomMonitor> getRoomMonitors() {
		return this.roomMonitors;
	}
	
	public EnvironmentMonitor getEnvironmentMonitor() {
		return this.environmentMonitor;
	}
	
	public HouseController getHouseController() {
		return this.houseController;
	}
}
